/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.capiq.client.view;

import java.util.List;

import com.excilys.capiq.client.service.QuestionServiceAsync;
import com.excilys.capiq.shared.UserScoreDTO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;

public class UserScoresWidget extends Composite {

    interface UserScoresBinder extends UiBinder<Widget, UserScoresWidget> {}

    private static UserScoresBinder    uiBinder = GWT.create(UserScoresBinder.class);

    private final QuestionServiceAsync questionService;

    @UiField
    FlexTable                          scoreTable;

    @UiField
    InlineLabel                        refreshing;

    @UiField
    Anchor                             refresh;

    public UserScoresWidget(QuestionServiceAsync questionService) {
        this.questionService = questionService;
        initWidget(uiBinder.createAndBindUi(this));

        refresh();
    }

    private void refresh() {

        refresh.setVisible(false);
        refreshing.setVisible(true);

        questionService.getUserScores(new AsyncCallback<List<UserScoreDTO>>() {

            @Override
            public void onSuccess(List<UserScoreDTO> result) {

                scoreTable.removeAllRows();

                /*
                 * Headers
                 */

                scoreTable.getRowFormatter().addStyleName(0, "scoreListHeader");

                int j = 0;
                scoreTable.setText(0, j++, "Rang");
                scoreTable.setText(0, j++, "Pseudo");
                scoreTable.setText(0, j++, "Score");
                scoreTable.setText(0, j++, "Bonnes réponses");
                scoreTable.setText(0, j++, "Total réponses");
                scoreTable.setText(0, j++, "Ratio");
                scoreTable.setText(0, j++, "Questions créées");

                for (int i = 0; i < j; i++) {
                    if (i == 1) {
                        continue;
                    }
                    scoreTable.getCellFormatter().addStyleName(0, i, "scoreListNumericColumn");
                }

                NumberFormat numberFormat = NumberFormat.getFormat("#.##");
                for (UserScoreDTO userScore : result) {

                    int row = scoreTable.getRowCount();

                    String ratioString;
                    if (userScore.getQuestionAnswered() != 0) {
                        double ratio = 100. * userScore.getGoodAnswers() / userScore.getQuestionAnswered();

                        ratioString = numberFormat.format(ratio) + "%";
                    } else {
                        ratioString = "N/A";
                    }

                    j = 0;
                    scoreTable.setText(row, j++, "" + row);
                    scoreTable.setText(row, j++, userScore.getName());
                    scoreTable.setText(row, j++, "" + userScore.getScore());
                    scoreTable.setText(row, j++, "" + userScore.getGoodAnswers());
                    scoreTable.setText(row, j++, "" + userScore.getQuestionAnswered());
                    scoreTable.setText(row, j++, ratioString);
                    scoreTable.setText(row, j++, "" + userScore.getQuestionCreated());

                    for (int i = 0; i < j; i++) {
                        if (i == 1) {
                            continue;
                        }
                        scoreTable.getCellFormatter().addStyleName(row, i, "scoreListNumericColumn");
                    }
                }
                refresh.setVisible(true);
                refreshing.setVisible(false);
                scoreTable.setVisible(true);

            }

            @Override
            public void onFailure(Throwable caught) {
                refresh.setVisible(true);
                refreshing.setVisible(false);
                Window.alert("Erreur " + caught.getMessage());
            }
        });

    }

    @UiHandler("refresh")
    public void refresh(ClickEvent event) {
        refresh();
    }

}
